package com.tkm.advancedFunctions.sam

import com.tkm.java.EventManager
import java.util.concurrent.Executors

//  fun interface：函数式接口，SAM
//  Java中的@FunctionalInterface：接口中只有一个方法，则可以使用@FunctionalInterface修饰
fun interface Clickable {
    fun onClick()
}

fun interface KRunnable {
    fun run()
}

fun main() {
    val clickable = Clickable { println("OnClicked") }
    clickable.onClick()

    val runnable = Runnable { println("Runnable") }
    runnable.run()

    val kRunnable = KRunnable { println("KRunnable") }
    kRunnable.run()

    val executor = Executors.newSingleThreadExecutor()

    //  完整的匿名内部类写法
    executor.submit(object : Runnable {
        override fun run() {
            println("object Runnable run")
        }
    })

    executor.submit(Runnable {
        println("Runnable run")
    })

    executor.submit(runnable)

    executor.submit { println("Runnable run") }

    val eventManager = EventManager()
    val eventListener = EventManager.OnEventListener {
        println("onEvent: $it")
    }

    //  onCreate
    eventManager.addListener(eventListener)
    eventListener.onEvent(114514)
    eventListener.onEvent(1919810)

    //  onDestroy
    eventManager.removeListener(eventListener)
}
